import * as cornerstoneTools from '@roos/cornerstone3d-tools';

import { getRenderingEngine } from '@cornerstonejs/core';
import { RenderingEngineId } from './constant';

const {
  Enums: csToolsEnums,
  annotation,
  PlanarRotateTool,
  ReferenceLinesTool,
  ArrowAnnotateTool,
  PanTool,
  ZoomTool,
  StackScrollMouseWheelTool,
  WindowLevelTool,
  LengthTool,
  HeightTool,
  AngleTool,
  CobbAngleTool,
  BidirectionalTool,
  ProbeTool,
  RectangleROITool,
  EraserTool,
  MagnifyTool,
  OrientationMarkerTool,
  CardiothoracicRatioTool,
  ScaleOverlayTool
} = cornerstoneTools;
const { state } = annotation;
const { MouseBindings, KeyboardBindings } = csToolsEnums;

//启用的ToolName池
const _toolPool: any = {
  ArrowAnnotate: ArrowAnnotateTool,
  Pan: PanTool,
  Zoom: ZoomTool,
  WindowLevel: WindowLevelTool,
  Length: LengthTool,
  Height: HeightTool,
  Angle: AngleTool,
  CobbAngle: CobbAngleTool,
  Bidirectional: BidirectionalTool,
  Probe: ProbeTool,
  RectangleROI: RectangleROITool,
  Eraser: EraserTool,
  PlanarRotate: PlanarRotateTool,
  Magnify: MagnifyTool,
  CardiothoracicRatio:CardiothoracicRatioTool
};

//判断是否是基石工具
export const isCornerstoneTool = (mark: string) => {
  return _toolPool[mark];
};

//绑定默认的工具并激活
export const bindDefaultTool = (toolGroup: any) => {
  toolGroup.addTool(PanTool.toolName);
  toolGroup.addTool(ZoomTool.toolName);
  toolGroup.addTool(StackScrollMouseWheelTool.toolName);
  toolGroup.addTool(WindowLevelTool.toolName);
  toolGroup.addTool(ScaleOverlayTool.toolName);
  toolGroup.addTool(ReferenceLinesTool.toolName);
  toolGroup.addTool(OrientationMarkerTool.toolName);
  toolGroup.setToolConfiguration(OrientationMarkerTool.toolName, {
    overlayMarkerType: OrientationMarkerTool.OVERLAY_MARKER_TYPES['ANNOTATED_CUBE'],
    orientationWidget: {
      enabled: true,
      viewportCorner: 'TOP_RIGHT',
      viewportSize: 0.15,
      minPixelSize: 80,
      maxPixelSize: 200
    }
  });

  toolGroup.setToolEnabled(OrientationMarkerTool.toolName);

  //默认绑定左键为移动工具
  toolGroup.setToolActive(PanTool.toolName, {
    bindings: [
      {
        mouseButton: MouseBindings.Primary
      }
    ]
  });
  //默认绑定右键为wwwc工具
  toolGroup.setToolActive(WindowLevelTool.toolName, {
    bindings: [
      {
        mouseButton: MouseBindings.Secondary
      }
    ]
  });
  //默认绑定滚轮按下为缩放工具
  toolGroup.setToolActive(ZoomTool.toolName, {
    bindings: [
      {
        mouseButton: MouseBindings.Auxiliary
      }
    ]
  });
  //默认绑定滚轮滚动为stack
  toolGroup.setToolActive(StackScrollMouseWheelTool.toolName);

  toolGroup.setToolConfiguration(ScaleOverlayTool.toolName, {
    scaleLocation: 'left'
  });
  toolGroup.setToolEnabled(ScaleOverlayTool.toolName);

};

export const setReferenceLinesViewportId = (toolGroup: any, selectedViewportId: string) => {
  toolGroup.setToolConfiguration(ReferenceLinesTool.toolName, {
    sourceViewportId: selectedViewportId,
    showFullDimension:true
  });
  toolGroup.setToolEnabled(ReferenceLinesTool.toolName);
};

export const setScaleOverlayViewportId = (toolGroup: any, selectedViewportId: string) => {
  // toolGroup.setToolDisabled(ScaleOverlayTool.toolName);
  toolGroup.setToolConfiguration(ScaleOverlayTool.toolName, {
    viewportId: selectedViewportId,
    scaleLocation: 'left'
  });
  toolGroup.setToolEnabled(ScaleOverlayTool.toolName);
};

//设置工具启用
export const setToolActive = (toolGroup: any, mark: string) => {
  if (!_toolPool[mark]) {
    return;
  }
  //判断当前是否启用工具，如果启用，则先将其弃用
  const preToolName = toolGroup.currentActivePrimaryToolName;
  if (preToolName) {
    toolGroup.setToolPassive(preToolName);
  }
  toolGroup.addTool(mark);
  //都绑定到左键
  toolGroup.setToolActive(mark, {
    bindings: [
      {
        mouseButton: MouseBindings.Primary
      }
    ]
  });
};

export const clearGeneralToolState = (toolGroup: any) => {
  const notClearToolNames = ['ScaleOverlay'];
  // const notClearToolNames = [''];
  const allAnnotations = state.getAllAnnotations();
  const removeAnnotations = allAnnotations.filter((e) => !notClearToolNames.includes(e.metadata.toolName));
  removeAnnotations.forEach((e) => state.removeAnnotation(e.annotationUID ?? ''));
  const renderingEngine = getRenderingEngine(RenderingEngineId);
  const viewports = renderingEngine?.getViewports() ?? [];
  viewports.forEach((v) => v.render());
};

export const clearScaleOverlayToolState = (toolGroup: any) => {
  const allAnnotations = state.getAllAnnotations();
  console.log('allAnnotations',allAnnotations)
  const removeAnnotations = allAnnotations.filter((e) => 'ScaleOverlay' == e.metadata.toolName);
  console.log('removeAnnotations',removeAnnotations)
  removeAnnotations.forEach((e) => state.removeAnnotation(e.annotationUID ?? ''));
  const renderingEngine = getRenderingEngine(RenderingEngineId);
  const viewports = renderingEngine?.getViewports() ?? [];
  viewports.forEach((v) => v.render());
};

export const clearAllToolState = () => {
  state.removeAllAnnotations();
};

//放弃已选工具
export const giveUp = (toolGroup: any) => {
  const activeTool = toolGroup.getActivePrimaryMouseButtonTool();
  toolGroup.setToolPassive(activeTool);
  toolGroup.setToolActive(PanTool.toolName, {
    bindings: [
      {
        mouseButton: MouseBindings.Primary
      }
    ]
  });
};
